package com.woniuxy.woniumall.controller;

import com.woniuxy.woniumall.entity.User;
import com.woniuxy.woniumall.service.CartService;
import com.woniuxy.woniumall.vo.CartVo;
import com.woniuxy.woniumall.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("cart")
@Slf4j
public class CartController {

    @Autowired
    CartService cartService ;

    @GetMapping("/addCart/{num}/{bookId}")
    @ResponseBody
    public ResponseResult<Void> addCart(@PathVariable("num") int num,
                                        @PathVariable("bookId") int bookId,
                                        HttpServletRequest request){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            User user = (User) request.getSession().getAttribute("userInfo");
            int i = cartService.addCart(num, bookId, user.getId());
            if (i == 1) {
                result.setCode(200);
                result.setMsg("添加购物车成功");
            }
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("添加购物车失败!");
        }

        return result ;
    }

    @GetMapping("/myCart")
    @ResponseBody
    public ResponseResult<List<CartVo>> findMyCart(HttpSession session){
        ResponseResult<List<CartVo>> result = new ResponseResult<>();
        try {
            User user = (User) session.getAttribute("userInfo");
            log.info("当前的用户:"+user.getUsername());
            List<CartVo> list = cartService.findMyCart(user);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(list);
        }catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result ;
    }

    @GetMapping("/modifyCart")
    @ResponseBody
    public ResponseResult<List<CartVo>> modifyCart(HttpSession session,int bookId,int num){
        ResponseResult<List<CartVo>> result = new ResponseResult<>();
        try {
            User user = (User) session.getAttribute("userInfo");
            List<CartVo> list = cartService.modifyMyCart(user,bookId,num);

            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(list);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result ;
    }

    @DeleteMapping("/del/{id}")
    @ResponseBody
    public ResponseResult<List<CartVo>> del(HttpSession session,@PathVariable("id") int id){
        ResponseResult<List<CartVo>> result = new ResponseResult<>();
        try {
            User user = (User) session.getAttribute("userInfo");
            List<CartVo> list = cartService.removeMyCartById(user,id);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(list);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result ;
    }

    @PostMapping("/cal")
    @ResponseBody
    public ResponseResult<List<CartVo>> cal(HttpSession session,Integer[] ids){
        ResponseResult<List<CartVo>> result = new ResponseResult<>();
        System.out.println(ids);
        return result ;
    }


    @PostMapping("/submitCart")
    @ResponseBody
    public ResponseResult<List<CartVo>> submitCart(HttpSession session,Integer[] ids){
        ResponseResult<List<CartVo>> result = new ResponseResult<>();
        try {
            User user = (User) session.getAttribute("userInfo");
            List<CartVo> list = cartService.findSubmitCart(user,ids);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(list);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result ;
    }


}